﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace DARE
{
    public class CHudTextureData: ISaveData<CHudTexture, CHudTexture>
    {
        public Rectangle m_rect;

        public string m_bodyTexture;
        public CAnimationState m_anim;
        public string m_spriteSheet;

        public Nullable<KeyValuePair<string, Rectangle>> m_topTexture;
        public Nullable<KeyValuePair<string, Rectangle>> m_bottomTexture;
        public Nullable<KeyValuePair<string, Rectangle>> m_leftTexture;
        public Nullable<KeyValuePair<string, Rectangle>> m_rightTexture;

        public Nullable<KeyValuePair<string, Rectangle>> m_topLeftTexture;
        public Nullable<KeyValuePair<string, Rectangle>> m_topRightTexture;
        public Nullable<KeyValuePair<string, Rectangle>> m_bottomLeftTexture;
        public Nullable<KeyValuePair<string, Rectangle>> m_bottomRightTexture;

        public void FillDataStructure(CHudTexture hudTexture)
        {
            if (hudTexture == null)
                return;
            m_bodyTexture = CResourceMgr.GetResourceName(hudTexture.Body);
            m_anim = hudTexture.AnimationState;
            m_rect = hudTexture.Rectangle;
            if (hudTexture.AnimationState.SpriteSheet != null)
                m_spriteSheet = CResourceMgr.GetResourceName(hudTexture.AnimationState.SpriteSheet);
            if (hudTexture.Top != null)
                m_topTexture = new KeyValuePair<string, Rectangle>(CResourceMgr.GetResourceName(hudTexture.Top.Value.Key), hudTexture.Top.Value.Value);
            if (hudTexture.Bottom != null)
                m_bottomTexture = new KeyValuePair<string, Rectangle>(CResourceMgr.GetResourceName(hudTexture.Bottom.Value.Key), hudTexture.Bottom.Value.Value);
            if (hudTexture.Left != null)
                m_leftTexture = new KeyValuePair<string, Rectangle>(CResourceMgr.GetResourceName(hudTexture.Left.Value.Key), hudTexture.Left.Value.Value);
            if (hudTexture.Right != null)
                m_rightTexture = new KeyValuePair<string, Rectangle>(CResourceMgr.GetResourceName(hudTexture.Right.Value.Key), hudTexture.Right.Value.Value);

            if (hudTexture.TopLeft != null)
                m_topLeftTexture = new KeyValuePair<string, Rectangle>(CResourceMgr.GetResourceName(hudTexture.TopLeft.Value.Key), hudTexture.TopLeft.Value.Value);
            if (hudTexture.TopRight != null)
                m_topRightTexture = new KeyValuePair<string, Rectangle>(CResourceMgr.GetResourceName(hudTexture.TopRight.Value.Key), hudTexture.TopRight.Value.Value);
            if (hudTexture.BottomLeft != null)
                m_bottomLeftTexture = new KeyValuePair<string, Rectangle>(CResourceMgr.GetResourceName(hudTexture.BottomLeft.Value.Key), hudTexture.BottomLeft.Value.Value);
            if (hudTexture.BottomRight != null)
                m_bottomRightTexture = new KeyValuePair<string, Rectangle>(CResourceMgr.GetResourceName(hudTexture.BottomRight.Value.Key), hudTexture.BottomRight.Value.Value);
        }

        public virtual void FillFromDataStructure(CHudTexture texture)
        {
            texture.Rectangle = m_rect;
            if (m_anim != null)
            {
                if (m_spriteSheet != null)
                    m_anim.SpriteSheet = CResourceMgr.Load<Texture2D>(m_spriteSheet);
                texture.AnimationState = m_anim.Clone();
            }
            if (m_bodyTexture != null)
                texture.Body = CResourceMgr.Load<Texture2D>(m_bodyTexture);

            if (m_topTexture != null && m_topTexture.Value.Key != null)
                texture.Top = new KeyValuePair<Texture2D,Rectangle>(CResourceMgr.Load<Texture2D>(m_topTexture.Value.Key), m_topTexture.Value.Value);
            if (m_bottomTexture != null && m_bottomTexture.Value.Key != null)
                texture.Bottom = new KeyValuePair<Texture2D, Rectangle>(CResourceMgr.Load<Texture2D>(m_bottomTexture.Value.Key), m_bottomTexture.Value.Value);
            if (m_leftTexture != null && m_leftTexture.Value.Key != null)
                texture.Left = new KeyValuePair<Texture2D, Rectangle>(CResourceMgr.Load<Texture2D>(m_leftTexture.Value.Key), m_leftTexture.Value.Value);
            if (m_rightTexture != null && m_rightTexture.Value.Key != null)
                texture.Right = new KeyValuePair<Texture2D, Rectangle>(CResourceMgr.Load<Texture2D>(m_rightTexture.Value.Key), m_rightTexture.Value.Value);

            if (m_topLeftTexture != null && m_topLeftTexture.Value.Key != null)
                texture.TopLeft = new KeyValuePair<Texture2D, Rectangle>(CResourceMgr.Load<Texture2D>(m_topLeftTexture.Value.Key), m_topLeftTexture.Value.Value);
            if (m_topRightTexture != null && m_topRightTexture.Value.Key != null)
                texture.TopRight = new KeyValuePair<Texture2D, Rectangle>(CResourceMgr.Load<Texture2D>(m_topRightTexture.Value.Key), m_topRightTexture.Value.Value);
            if (m_bottomLeftTexture != null && m_bottomLeftTexture.Value.Key != null)
                texture.BottomLeft = new KeyValuePair<Texture2D, Rectangle>(CResourceMgr.Load<Texture2D>(m_bottomLeftTexture.Value.Key), m_bottomLeftTexture.Value.Value);
            if (m_bottomRightTexture != null && m_bottomRightTexture.Value.Key != null)
                texture.BottomRight = new KeyValuePair<Texture2D, Rectangle>(CResourceMgr.Load<Texture2D>(m_bottomRightTexture.Value.Key), m_bottomRightTexture.Value.Value);
        }
    }
}
